﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(QStringLiteral("客户端"));
    socket = new QTcpSocket();
    QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data); //读取
    QObject::connect(socket,&QTcpSocket::disconnected,this,&MainWindow::socket_DisConnection); //断开连接

    ui->pushButton_Send->setEnabled(false);
    ui->lineEdit_IP->setText("127.0.0.1"); //IP
    ui->lineEdit_Port->setText("8848"); //端口
}

MainWindow::~MainWindow()
{
    delete ui;
}
/*************************************************
     Description:    连接及断开服务器
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::on_pushButton_Connection_clicked()
{
    if(ui->pushButton_Connection->text() == QString (QStringLiteral("连接")))
    {
        QString IP;
        int port;
        IP = ui->lineEdit_IP->text();
        port = ui->lineEdit_Port->text().toInt();
        //取消已有连接
        socket->abort();
        //连接服务器
        socket->connectToHost(IP,port);
        //等待连接成功
        if(!socket->waitForConnected(30000))
        {
            qDebug()<<"Connection failed!";
            return;
        }
        qDebug()<<"Connection success!";
        //发送按键使能
        ui->pushButton_Send->setEnabled(true);
        //修改按键文字
        ui->pushButton_Connection->setText(QStringLiteral("断开连接"));
    }
    else
    {
     //断开连接
     socket->disconnectFromHost();
     ui->pushButton_Connection->setText(QStringLiteral("连接"));
     ui->pushButton_Send->setEnabled(false);
    }
}
/*************************************************
     Description:    向服务器发送消息
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::on_pushButton_Send_clicked()
{
    qDebug()<<"Send: "<<ui->textEdit_Send->toPlainText();
    socket->write(ui->textEdit_Send->toPlainText().toLocal8Bit());
    socket->flush();
}
/*************************************************
     Description:    接收及读取服务器消息
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::socket_Read_Data()
{
    QByteArray buffer;
    //读取数据缓冲区数据
    buffer = socket->readAll();
    if(!buffer.isEmpty()){
        QString str = ui->textEdit_Recv->toPlainText();
        QString strRecv = QString::fromLocal8Bit(buffer);
        str+=strRecv+"\n";
        //刷新显示
        ui->textEdit_Recv->setText(str);
    }
}
/*************************************************
     Description:    断开连接后
     return:         void
     Auther:         任占宇
     Time:           2021-1-30
   *************************************************/
void MainWindow::socket_DisConnection()
{
    //发送按键失能
    ui->pushButton_Send->setEnabled(false);
    ui->pushButton_Connection->setText(QStringLiteral("连接"));
    qDebug()<<"Disconnection!";
}
